package io.itit.ecp.admin.provide.action.controller;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import icu.mhb.mybatisplus.plugln.core.JoinLambdaWrapper;
import icu.mhb.mybatisplus.plugln.extend.Joins;
import io.itit.ecp.admin.provide.action.req.EnergyForecastDataQueryReq;
import io.itit.ecp.admin.provide.action.resp.EnergyForecastDataResp;
import io.itit.ecp.admin.provide.filter.IEnergyDataFilter;
import io.itit.ecp.admin.provide.helper.SceneHelper;
import io.itit.ecp.admin.server.entity.EnergyForecastDataEntity;
import io.itit.ecp.admin.server.entity.ProductTypeEntity;
import io.itit.ecp.admin.server.entity.SceneProjectEntity;
import io.itit.ecp.admin.server.service.IEnergyForecastDataService;
import io.itit.grass.common.provide.action.req.BaseBatchIdReq;
import io.itit.grass.common.provide.action.req.BaseIdReq;
import io.itit.grass.common.provide.action.resp.ResultResp;
import io.itit.grass.common.provide.action.validation.IExport;
import io.itit.grass.common.provide.action.validation.IQueryPage;
import io.itit.grass.common.provide.action.validation.IRequired;
import io.itit.grass.estelle.starter.core.ExcelXSSFUtil;
import io.itit.grass.estelle.starter.util.JsonUtil;
import io.itit.grass.web.wall.utils.PageUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * 能源预测数据
 */
@Api(tags = "能源预测数据")
@RestController
@RequestMapping("/ecp-admin/energyForecastData")
public class EnergyForecastDataController {

    @Resource
    private IEnergyForecastDataService energyForecastDataService;

    @ApiOperation("分页查询")
    @PostMapping("queryPage")
    @RequiresPermissions("ecp-admin:energyForecastData:search")
    public ResultResp<IPage<EnergyForecastDataResp>> queryPage(@RequestBody @Validated(IQueryPage.class) EnergyForecastDataQueryReq req) {
        Page<EnergyForecastDataResp> forecastDataRespPage = buildQW(req)
                .joinPage(PageUtils.getPage(req, EnergyForecastDataResp.class), EnergyForecastDataResp.class);
        return ResultResp.success(SceneHelper.i().setSceneInfo(forecastDataRespPage));
    }

    private static JoinLambdaWrapper<EnergyForecastDataEntity> buildQW(EnergyForecastDataQueryReq req) {
        String start = req.getSolidifyDateStart();
        String end = req.getSolidifyDateEnd();
        Date startDate = null, endDate = null;
        if (StringUtils.hasLength(start)) {
            startDate = DateUtil.parse(start, DatePattern.NORM_DATETIME_FORMAT);
        }
        if (StringUtils.hasLength(end)) {
            endDate = DateUtil.parse(end, DatePattern.NORM_DATETIME_FORMAT);
        }
        return Joins.of(EnergyForecastDataEntity.class)
                .leftJoin(SceneProjectEntity.class, SceneProjectEntity::getId, EnergyForecastDataEntity::getSceneProjectId)
                .selectAs(SceneProjectEntity::getProjectName, EnergyForecastDataResp::getProjectName)
                .end()
                .leftJoin(ProductTypeEntity.class, ProductTypeEntity::getId, EnergyForecastDataEntity::getProductTypeId)
                .selectAs(ProductTypeEntity::getProductTypeName, EnergyForecastDataResp::getProductTypeName)
                .end()
                .eq(StringUtils.hasLength(req.getSceneProjectId()), EnergyForecastDataEntity::getSceneProjectId, req.getSceneProjectId())
                .eq(StringUtils.hasLength(req.getSceneEdificeId()), EnergyForecastDataEntity::getSceneEdificeId, req.getSceneEdificeId())
                .eq(StringUtils.hasLength(req.getSceneFloorId()), EnergyForecastDataEntity::getSceneFloorId, req.getSceneFloorId())
                .eq(StringUtils.hasLength(req.getSceneRoomId()), EnergyForecastDataEntity::getSceneRoomId, req.getSceneRoomId())
                .ge(Objects.nonNull(startDate), EnergyForecastDataEntity::getSolidifyDate, startDate)
                .le(Objects.nonNull(endDate), EnergyForecastDataEntity::getSolidifyDate, endDate)
                .eq(StringUtils.hasLength(req.getDeviceName()), EnergyForecastDataEntity::getDeviceName, req.getDeviceName())
                .orderByDesc(EnergyForecastDataEntity::getSolidifyDate);
    }

    @ApiOperation("删除(根据Id删)")
    @PostMapping("deleteById")
    @RequiresPermissions("ecp-admin:energyForecastData:deleteById")
    public ResultResp<Boolean> deleteById(@RequestBody @Validated({IRequired.class}) BaseIdReq req) {
        return ResultResp.success(energyForecastDataService.removeById(req.getId()));
    }

    @ApiOperation("批量删除")
    @PostMapping("batchDelete")
    @RequiresPermissions("ecp-admin:energyForecastData:batchDelete")
    public ResultResp<Boolean> batchDelete(@RequestBody @Validated({IRequired.class}) BaseBatchIdReq req) {
        return ResultResp.success(energyForecastDataService.removeBatchByIds(req.getIds(), 500));
    }

    @ApiOperation("导出")
    @PostMapping("export")
    @RequiresPermissions("ecp-admin:energyForecastData:export")
    public void export(@RequestBody @Validated(IExport.class) EnergyForecastDataQueryReq req, HttpServletResponse
            response) {
        List<EnergyForecastDataResp> respList = SceneHelper.i().setSceneInfo(buildQW(req).joinList(EnergyForecastDataResp.class));
        ExcelXSSFUtil.export("EnergyForecastDataExport", JsonUtil.toJson(respList, new IEnergyDataFilter.EnergyDataExtExportFilter()), response);
    }


}
